<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${buildCodeRequest.codePackageDTO.daoPackage}.${buildCodeRequest.codeClassNameDTO.getDaoClassName()}">

    <resultMap id="${buildCodeRequest.codeClassNameDTO.getDomainClassName()}" type = "${buildCodeRequest.codePackageDTO.getDomainPackage()}.${buildCodeRequest.codeClassNameDTO.getDomainClassName()}">
        <id column="${buildCodeRequest.primaryAttributeDTO.columnAttribute.columnName}" property="${buildCodeRequest.primaryAttributeDTO.javaAttribute.attributeName}" />
        <#list buildCodeRequest.attributeDTOList as attributeDTO>
        <#if attributeDTO.columnAttribute.columnName!= buildCodeRequest.primaryAttributeDTO.columnAttribute.columnName>
        <result column="${attributeDTO.columnAttribute.columnName}" property="${attributeDTO.javaAttribute.attributeName}" />
        </#if>
        </#list>
    </resultMap>
    <#assign columnList = "">
    <#list buildCodeRequest.attributeDTOList as attributeDTO>
        <#if columnList != "">
            <#assign columnList = columnList + ", ">
        </#if>
        <#assign columnList = columnList +  attributeDTO.columnAttribute.columnName>
    </#list>

    <sql id="all_columns">
        <![CDATA[${columnList}]]>
    </sql>

    <#noparse>
    <sql id="select_columns">
        <choose>
            <when test="specifiedColumns != null"> ${specifiedColumns} </when>
            <otherwise> <include refid="all_columns" /> </otherwise>
        </choose>
    </sql>
    </#noparse>

    <sql id="query_condition">
    <#list buildCodeRequest.attributeDTOList as attributeDTO>
        <#if attributeDTO.javaAttribute.supportEqual?? && attributeDTO.javaAttribute.supportEqual=="Y">
        <if test="${attributeDTO.javaAttribute.attributeName} != null"><![CDATA[ AND ${attributeDTO.columnAttribute.columnName}  = <#noparse>#{</#noparse>${attributeDTO.javaAttribute.attributeName}<#noparse>}</#noparse>]]></if>
        </#if>
        <#if attributeDTO.javaAttribute.supportFuzzy?? && attributeDTO.javaAttribute.supportFuzzy=="Y">
        <if test="fuzzy${attributeDTO.javaAttribute.initUpperAttributeName} != null"><![CDATA[ AND ${attributeDTO.columnAttribute.columnName} LIKE CONCAT('%', <#noparse>#{</#noparse>fuzzy${attributeDTO.javaAttribute.initUpperAttributeName}<#noparse>}</#noparse>,'%')]]></if>
        </#if>
        <#if attributeDTO.javaAttribute.supportIn?? && attributeDTO.javaAttribute.supportIn=="Y">
        <if test="${attributeDTO.javaAttribute.attributeName}Set != null and ${attributeDTO.javaAttribute.attributeName}Set.size()!=0">
            AND ${attributeDTO.columnAttribute.columnName} IN
            <foreach item="item" index="index" collection="${attributeDTO.javaAttribute.attributeName}Set" open="(" separator="," close=")">
                <#noparse>#{item}</#noparse>
            </foreach>
        </if>
        </#if>
        <#if attributeDTO.javaAttribute.supportCompare?? && attributeDTO.javaAttribute.supportCompare=="Y">
        <if test="lessThan${attributeDTO.javaAttribute.initUpperAttributeName} != null"><![CDATA[ AND ${attributeDTO.columnAttribute.columnName} < <#noparse>#{</#noparse>lessThan${attributeDTO.javaAttribute.initUpperAttributeName}<#noparse>}</#noparse>]]></if>
        <if test="equalAndLessThan${attributeDTO.javaAttribute.initUpperAttributeName} != null"><![CDATA[ AND ${attributeDTO.columnAttribute.columnName} <= <#noparse>#{</#noparse>equalAndLessThan${attributeDTO.javaAttribute.initUpperAttributeName}<#noparse>}</#noparse>]]></if>
        <if test="moreThan${attributeDTO.javaAttribute.initUpperAttributeName} != null"><![CDATA[ AND ${attributeDTO.columnAttribute.columnName}> <#noparse>#{</#noparse>moreThan${attributeDTO.javaAttribute.initUpperAttributeName}<#noparse>}</#noparse>]]></if>
        <if test="equalAndMoreThan${attributeDTO.javaAttribute.initUpperAttributeName} != null"><![CDATA[ AND ${attributeDTO.columnAttribute.columnName}>= <#noparse>#{</#noparse>equalAndMoreThan${attributeDTO.javaAttribute.initUpperAttributeName}<#noparse>}</#noparse>]]></if>
        </#if>
    </#list>
<#noparse>
        <if test="nullCheckedAttributes != null and nullCheckedAttributes.size() > 0">
            <foreach collection="nullCheckedAttributes" item="attr" separator="AND">
                ${attr} IS NULL
            </foreach>
        </if>
        <if test="nonNullCheckedAttributes != null and nonNullCheckedAttributes.size() > 0">
            <foreach collection="nonNullCheckedAttributes" item="attr" separator="AND">
                ${attr} IS NOT NULL
            </foreach>
        </if>
 </#noparse>
    </sql>
    <#assign columnList = "">
    <#list buildCodeRequest.attributeDTOList as attributeDTO>
        <#if columnList != "">
            <#assign columnList = columnList + " , ">
        </#if>
        <#assign columnList = columnList + "#{" + attributeDTO.javaAttribute.attributeName + "}">
    </#list>

    <insert id="${buildCodeRequest.codeMethodDTO.insertMethod}" parameterType="${buildCodeRequest.codePackageDTO.getDomainPackage()}.${buildCodeRequest.codeClassNameDTO.getDomainClassName()}" useGeneratedKeys="true" keyProperty="${buildCodeRequest.primaryAttributeDTO.javaAttribute.attributeName}">
        INSERT INTO ${buildCodeRequest.tableName} (<include refid="all_columns" />) VALUES 
        (${columnList})
    </insert>


    <select id="${buildCodeRequest.codeMethodDTO.queryListMethod}" resultMap="${buildCodeRequest.codeClassNameDTO.getDomainClassName()}" parameterType="${buildCodeRequest.codePackageDTO.queryPackage}.${buildCodeRequest.codeClassNameDTO.queryClassName}">
        SELECT <include refid="select_columns" /> FROM ${buildCodeRequest.tableName}
        <where>
            <include refid="query_condition"/>
        </where>
        <#noparse><if test="orderByClause != null"> ORDER BY ${orderByClause}</if></#noparse>
        <!-- add order or query info. -->
        <![CDATA[

        ]]>
        <#noparse><if test="startNum != null"></#noparse> <#if !buildCodeRequest.dbType?? || buildCodeRequest.dbType == "mysql"><#noparse>LIMIT #{startNum}, #{pageSize}</#noparse> <#else> <#noparse>LIMIT #{pageSize} OFFSET #{startNum}</#noparse></#if></if>
    </select>

<#noparse>
    <sql id="count_column">
        <choose>
            <when test="countAttributeName != null"> ${countAttributeName} </when>
            <otherwise> 1 </otherwise>
        </choose>
    </sql>
</#noparse>

    <select id="${buildCodeRequest.codeMethodDTO.countMethod}" resultType="int" parameterType="${buildCodeRequest.codePackageDTO.queryPackage}.${buildCodeRequest.codeClassNameDTO.queryClassName}">
        SELECT COUNT(<include refid="count_column" />) FROM ${buildCodeRequest.tableName}
        <where>
            <include refid="query_condition"/>
        </where>
        <!-- add order or query info. -->
        <![CDATA[
        ]]>
    </select>

    <select id="selectDistinctList" resultType="String" parameterType="${buildCodeRequest.codePackageDTO.queryPackage}.${buildCodeRequest.codeClassNameDTO.queryClassName}">
        SELECT DISTINCT( <#noparse>${distinct}</#noparse> ) FROM  ${buildCodeRequest.tableName}
        <where>
            <include refid="query_condition"/>
        </where>
    </select>

    <select id="${buildCodeRequest.codeMethodDTO.queryByIdMethod}" resultMap="${buildCodeRequest.codeClassNameDTO.getDomainClassName()}" parameterType="${buildCodeRequest.primaryAttributeDTO.javaAttribute.attributeType}">
        SELECT <include refid="all_columns" /> FROM ${buildCodeRequest.tableName} WHERE ${buildCodeRequest.primaryAttributeDTO.columnAttribute.columnName} = <#noparse>#{</#noparse>${buildCodeRequest.primaryAttributeDTO.javaAttribute.attributeName}<#noparse>}</#noparse>
    </select>

    <update id="${buildCodeRequest.codeMethodDTO.updateByIdMethod}" parameterType="${buildCodeRequest.codePackageDTO.getDomainPackage()}.${buildCodeRequest.codeClassNameDTO.getDomainClassName()}">
        UPDATE ${buildCodeRequest.tableName}
        <set>
            <#list buildCodeRequest.attributeDTOList as attributeDTO>
                <#if attributeDTO.columnAttribute.columnName == buildCodeRequest.primaryAttributeDTO.columnAttribute.columnName>
                <#elseif buildCodeRequest.updateVersionAttribute?? && attributeDTO.columnAttribute.columnName == buildCodeRequest.updateVersionAttribute.columnAttribute.columnName>
            <![CDATA[ UPDATE_VERSION = UPDATE_VERSION+1 ,]]>
                <#else>
            <if test="${attributeDTO.javaAttribute.attributeName} != null"><![CDATA[${attributeDTO.columnAttribute.columnName}=<#noparse>#{</#noparse>${attributeDTO.javaAttribute.attributeName}<#noparse>}</#noparse>,]]></if>
                </#if>
            </#list>
        </set>
            <![CDATA[ WHERE ${buildCodeRequest.primaryAttributeDTO.columnAttribute.columnName} = <#noparse>#{</#noparse>${buildCodeRequest.primaryAttributeDTO.javaAttribute.attributeName}<#noparse>}</#noparse>]]>
        <#if buildCodeRequest.updateVersionAttribute??>
            <if test="updateVersion != null"><![CDATA[ AND UPDATE_VERSION = #{updateVersion}]]></if>
        </#if>
    </update>

    <delete id="${buildCodeRequest.codeMethodDTO.deleteByIdMethod}">
        DELETE FROM ${buildCodeRequest.tableName} WHERE ${buildCodeRequest.primaryAttributeDTO.columnAttribute.columnName} = <#noparse>#{</#noparse>${buildCodeRequest.primaryAttributeDTO.javaAttribute.attributeName}<#noparse>}</#noparse>
    </delete>

</mapper>